/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Loanverification;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class LoanverificationDAO extends DAO {

    public LoanverificationDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Loanverification> getAll() throws Exception {
        List<Loanverification> a = (List<Loanverification>) getSession().createQuery("from Loanverification a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Loanverification get(int id) throws Exception {
        Loanverification a = (Loanverification) getSession().createQuery("from Loanverification a where a.loanverificationid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Loanverification a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Loanverification a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Loanverification> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Loanverification> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    @SuppressWarnings("unchecked")
    public boolean addForNew(Documents d, Borrowers b, Loanverification lv) throws Exception {
        try {
            Session session = getSession();
            begin();
            session.save(b);
            session.save(d);
            session.save(lv);
            commit();
            flush();
            return true;
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }

    @SuppressWarnings("unchecked")
    public boolean addForOld(Documents d, Loanverification lv) throws Exception {
        try {
            Session session = getSession();
            begin();
            session.save(d);
            session.save(lv);

            commit();
            flush();
            return true;
        } catch (Exception ex) {
            rollback();
            ex.printStackTrace();
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    public Loanverification getLatestloanver(int borid) throws Exception {
        Loanverification lv = (Loanverification) getSession().createQuery("from Loanverification l where l.borrowers.borrowerid=:bid order by l.loanverificationid desc").setParameter("bid", borid).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return lv;
    }

    @SuppressWarnings("unchecked")
    public Loanverification getById(int lvid) throws Exception {
        Loanverification lv = (Loanverification) getSession().createQuery("from Loanverification l where l.loanverificationid=:lvid").setParameter("lvid", lvid).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return lv;
    }

    @SuppressWarnings("unchecked")
    public List<Loanverification> getLoanAppsAF() throws Exception {
        String status = "saved";
        String withaf = "n";
        List<Loanverification> loanapplist = (List<Loanverification>) getSession().createQuery("from Loanverification l where l.status = :status and l.withaf = :withaf").setParameter("status", status).setParameter("withaf", withaf).setCacheable(true).list();
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Loanverification> getLoanAppsAP() throws Exception {
        String status = "saved";
        String withap = "n";
        List<Loanverification> loanapplist = getSession().createQuery("from Loanverification l where l.status = :status and l.withap = :withap").setParameter("status", status).setParameter("withap", withap).setCacheable(true).list();
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Loanverification> getLoanAppsCI() throws Exception {
        String status = "saved";
        String withci = "n";
        List<Loanverification> loanapplist = getSession().createQuery("from Loanverification l where l.status = :status and l.withci = :withci").setParameter("status", status).setParameter("withci", withci).setCacheable(true).list();
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Loanverification> getLoanAppsCM() throws Exception {
        String status = "saved";
        String withcm = "n";
        List<Loanverification> loanapplist = getSession().createQuery("from Loanverification l where l.status = :status and l.withcm = :withcm").setParameter("status", status).setParameter("withcm", withcm).setCacheable(true).list();
        flush();
        return loanapplist;
    }
}
